package com.io.convert;

import java.io.*;
import java.util.Scanner;

/**
 * PrintStream 打印流--》处理流
 *
 * @author jiangbang
 * @date 2016/6/3
 */
public class PrintStreamDemo {

    public static void main(String[] args) throws FileNotFoundException {
        test3();
    }

    /**
     * PrintStream 打印流--》处理流
     * 基本使用
     */
    public static void test1() throws FileNotFoundException {
        System.out.println("123");
        PrintStream ps = System.out;
        ps.print("123");

        //输出到文件
        File file = new File("D:/demo/2.txt");
        ps  = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
        ps.println("io is so easy");
        ps.close();
    }


    /**
     * 三个常量
     * 1.system.in 输入流
     * 2.system.out  输出流   控制台输出
     * system.err
     */
    public static void test2() throws FileNotFoundException {
        InputStream is = System.in; //键盘输入
        is = new BufferedInputStream(new FileInputStream("D:/demo/2.txt"));  //文件输入
        Scanner sc = new Scanner(is);
        System.out.println(sc.nextLine());
    }

    /**
     * 重定向   从控制台输入转到文件输入
     * setIn()
     * setOUt()
     * serErr()
     */
    public static void test3() throws FileNotFoundException {
        //重定向
        System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("D:/demo/2.txt")),true));
        System.out.println("1");
        System.out.println("adf");
        System.out.println("肩膀");
        System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true));
        System.out.println("1");
    }

}
